import 'package:flutter/material.dart';
import 'package:flutter_project/components/MTFoodItem.dart';

class MTBody extends StatefulWidget {
  const MTBody({Key? key, required this.list}) : super(key: key);
  final List list;
  @override
  _MTBodyState createState() => _MTBodyState();
}

class _MTBodyState extends State<MTBody> {
  int _activeIndex = 0;

  int getChildCount() {
    if (widget.list.length > 0) {
      return widget.list[_activeIndex]["foods"].length;
    }
    return 0;
  }

  List<Widget> getCategory() {
    List<Widget> list = [];
    for (var i = 0; i < widget.list.length; i++) {
      list.add(GestureDetector(
        onTap: () {
          print('触发点击事件');
        },
        child: Container(
          color: Colors.white,
          height: 80,
          alignment: Alignment.center,
          child: Text(widget.list[i]["name"]),
        ),
      ));
    }
    return list;
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      color: const Color.fromARGB(255, 48, 205, 95),
      width: double.infinity,
      height: double.infinity,
      child: Row(
        children: [
          Container(
            width: 120,
            color: Color.fromARGB(255, 209, 206, 206),
            child: ListView.separated(
                itemBuilder: (BuildContext context, int index) {
                  return GestureDetector(
                    onTap: () {
                      setState(() {
                        _activeIndex = index;
                      });
                    },
                    child: Container(
                      color: _activeIndex == index
                          ? Colors.white
                          : Colors.transparent,
                      height: 80,
                      alignment: Alignment.center,
                      child: Text(widget.list[index]["name"]),
                    ),
                  );
                },
                separatorBuilder: (BuildContext context, int index) {
                  return Container(
                    height: 1,
                    color: Colors.white,
                  );
                },
                itemCount: widget.list.length),
          ),
          Expanded(
            child: ListView.builder(
              itemBuilder: (BuildContext context, int index) {
                return MTFoodItem(
                    foodItem: widget.list[_activeIndex]["foods"][index]);
              },
              itemCount: getChildCount(),
            ),
          ),
        ],
      ),
    );
  }
}
